﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;



namespace thePayAPP
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private string userName;
        private void txtUserName_TextChanged(object sender, TextChangedEventArgs e)
        {
            userName = userNameTextBox.Text;
            iPay = 0;
        }

        private int iPay;

        private void LinkSendButtonClick(object sender, RoutedEventArgs e)
        {
            //1.检查用户是否正确
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show(this, "充值用户没有设置哦!");
                return;
            }
            //2.对用户名进行空格检查
            userName = userName.Trim();
            //3.充值确认
            if (MessageBox.Show(this, string.Format("将会为：{0}充值，是执行充值，否取消本次充值！", userName), "充值确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                try
                {
                    //执行联网充值
                    string rs = MuYue.Http.WebEngine.DownloadString(string.Format("http://mrsoft.duapp.com/dati/pay.jsp?user={0}&key=Mrsoft_SZW87827", userName));
                    iPay++;
                    rs = string.Format("{2}:第{0}次为{3}充值\r\n{1}", iPay, rs.Trim().Replace("\r\n", ""), DateTime.Now, userName);
                    //UI日志更新
                    Loglist.Items.Add(rs);
                    //日志记录
                    File.AppendAllText("pay.log", string.Format("{0}\r\n", rs));
                }
                catch (Exception ex)
                {
                    //异常捕获UI告知
                    Loglist.Items.Add("充值失败原因:" + ex.Message);
                }
            }
            else
            {
                //取消了本次充值
                Loglist.Items.Add("取消本次充值");
            }
        }
    }
}
